/**
 * 学生student的接口
 */
import express from "express";
import { DouyinTableProps, StudentProps, UserProps } from "./types";

const app = express();

app.use((request, _, next) => {
  console.log("有人请求服务器了");
  console.log("请求头的host", request.get("host"));
  console.log("请求的地址", request.url);
  next();
});

app.get("/students", (_, response) => {
  const students: StudentProps[] = [
    { id: "001", name: "小张", age: 18 },
    { id: "002", name: "小李", age: 19 },
    { id: "003", name: "小红", age: 20 }
  ];
  response.send(students);
});

app.get("/users", (request, response) => {
  const { body, query } = request;
  console.log(body, query);
  const users: UserProps[] = [
    { name: "小明", height: "180", gender: "男" },
    { name: "小李", height: "178", gender: "女" },
    { name: "小徐", height: "177", gender: "男" }
  ];

  response.send(users);
});

const data: DouyinTableProps[] = [
  {
    key: "1",
    name: "Semi Design 设计稿.fig",
    size: "2M",
    owner: "姜鹏志",
    updateTime: "2020-02-02 05:13"
  },
  {
    key: "2",
    name: "Semi Design 分享演示文稿",
    size: "2M",
    owner: "郝宣",
    updateTime: "2020-01-17 05:31"
  },
  {
    key: "3",
    name: "设计文档",
    size: "34KB",
    owner: "Zoey Edwards",
    updateTime: "2020-01-26 11:01"
  }
];

app.get("/douyin/table/query", (_, response) => {
  response.send(data);
});

app.put("/douyin/table/update", (request, response) => {
  // const { key, name } = JSON.parse(request.body) as { key: string; name: string };
  data.forEach(item => {
    if (item.key === "1") {
      item.name = "更换后";
    }
    return item;
  });
  response.send("ok");
});

app.listen(5000, () => {
  console.log("服务器启动成了，请求学生信息地址为：http://localhost:5000/students");
});
